#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

i64 f[101][101][101];
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  string str;
  cin >> str;
  str.insert(str.begin(), ' ');
  vector<int> w(n + 1);
  for (int i = 1; i <= n; i ++) cin >> w[i];
  vector<vector<i64>> dp(n + 1, vector<i64>(n + 1));
  for (int l = n; l >= 1; l --) {
    for (int r = l; r <= n; r ++) {
      for (int k = l; k < r; k ++) {
        dp[l][r] = max(dp[l][r], dp[l][k] + dp[k + 1][r]);
      }
      memset(f[l][r], 0x80, sizeof(f[l][r]));
      if (str[l] == str[r]) {
        if (l == r) f[l][r][1] = 0;
        for (int k = l; k < r; k ++) {
          if (str[k] == str[l]) {
            for (int i = 2; i <= n; i ++) {
              f[l][r][i] = max(f[l][r][i], f[l][k][i - 1] + dp[k + 1][r - 1]);
            }
          }
        }
        for (int i = 1; i <= n; i ++) {
          dp[l][r] = max(dp[l][r], f[l][r][i] + w[i]);
        }
      }
    }
  }
  debug(dp);
  cout << dp[1][n] << endl;
} 